package Task_010404;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DaoImpl extends DBUtils implements Dao {

    //private List<Flight> flightList = new ArrayList<>();//存结果集
    //private List<Object> paramList = new ArrayList<>();//存查询参数
    //private Flight f = new Flight();

    /**
     * 查询所有航班
     * @return
     */
    @Override
    public List<Flight> findAll() {
        List<Flight> flightList = new ArrayList<>();//存结果集

        String sql = "select * from flight";
        resultSet = query(sql,null);

        try {
            while(resultSet.next()){
                Flight f = new Flight();
                f.setId(resultSet.getString("id"));
                f.setNumber(resultSet.getString("number"));
                f.setDest(resultSet.getString("dest"));
                f.setDate(resultSet.getString("date"));
                flightList.add(f);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();//关闭所有资源
        }

        return flightList;
    }


    /**
     * 按起飞时间查询
     * @param date
     * @return
     */
    @Override
    public List<Flight> findByDate(String date) {
        List<Flight> flightList = new ArrayList<>();//存结果集
        List<Object> paramList = new ArrayList<>();//存查询参数

        String sql = "select * from flight where date=?";
        paramList.add(date);
        resultSet = query(sql,paramList);

        try {
            while(resultSet.next()){
                Flight f = new Flight();
                f.setId(resultSet.getString("id"));
                f.setNumber(resultSet.getString("number"));
                f.setDest(resultSet.getString("dest"));
                f.setDate(resultSet.getString("date"));
                flightList.add(f);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();//关闭所有资源
        }

        return flightList;
    }


    /**
     * 按目的地查询
     * @param dest
     * @return
     */
    @Override
    public List<Flight> findByDest(String dest) {
        List<Flight> flightList = new ArrayList<>();//存结果集
        List<Object> paramList = new ArrayList<>();//存查询参数

        String sql = "select * from flight where dest like ?";
        paramList.add("%"+dest+"%");
        resultSet = query(sql,paramList);

        try {
            while(resultSet.next()){
                Flight f = new Flight();
                f.setId(resultSet.getString("id"));
                f.setNumber(resultSet.getString("number"));
                f.setDest(resultSet.getString("dest"));
                f.setDate(resultSet.getString("date"));
                flightList.add(f);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();//关闭所有资源
        }

        return flightList;
    }


    /**
     * 删除航班信息
     * @param id
     */
    @Override
    public void del(String id) {
        List<Object> paramList = new ArrayList<>();//存查询参数

        String sql = "delete from flight where id=?";
        paramList.add(id);
        update(sql,paramList);
        close();
    }


    /**
     * 更新航班信息
     * @param f
     * @param id
     */
    @Override
    public void mod(Flight f,String id) {
        List<Object> paramList = new ArrayList<>();//存查询参数

        String sql = "update flight set number=?, dest=?, date=? where id=?";
        paramList.add(f.getNumber());
        paramList.add(f.getDest());
        paramList.add(f.getDate());
        paramList.add(id);
        update(sql,paramList);
        close();
    }


    /**
     * 按编号查询
     * @param id
     * @return
     */
    @Override
    public List<Flight> findById(String id) {
        List<Flight> flightList = new ArrayList<>();//存结果集
        List<Object> paramList = new ArrayList<>();//存查询参数

        String sql = "select * from flight where id=?";
        paramList.add(id);
        resultSet = query(sql,paramList);

        try {
            while(resultSet.next()){
                Flight f = new Flight();
                f.setId(resultSet.getString("id"));
                f.setNumber(resultSet.getString("number"));
                f.setDest(resultSet.getString("dest"));
                f.setDate(resultSet.getString("date"));
                flightList.add(f);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();//关闭所有资源
        }

        return flightList;
    }

}
